# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: blubin
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;

class PublishingBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "BookPublishing", dashboard=True);

    def wire(self):
        self.register_result_listener("Screener", "reviewmanuscript", self.manuscript_approval_complete)
        self.register_result_listener("Editor", "editmanuscript", self.edit_complete)
        #self.register_result_listener("GraphicDesigner", "coverdesign", self.cover_complete)
        self.register_result_listener("Supervisor", "approveedit", self.approval_complete)
        #self.register_result_listener("Supervisor", "setprice", self.Set_price_complete)
        self.register_result_listener("Publisher", "publishbook", self.publishing_complete)
        
    def manuscript_approval_complete(self, results):
        for result in results:
            if result.data['screenresult'] == "Pass":
                task = Task.construct_from_result(result, "Editor", "editmanuscript");
                self.workflow.add(task);
            else:
                pass
            self.workflow.update_status(result, Status.COMPLETE);

            
    def edit_complete(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Supervisor", "approveedit");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

#    def cover_complete(self, results):
#        for result in results:
#            task = Task.construct_from_result(result, "Supervisor", "approveedit");
#            self.workflow.add(task);
#            self.workflow.update_status(result, Status.COMPLETE);
   
    def approval_complete(self, results):
        for result in results:
            if result.data['editstatus'] == "Pass":
                task = Task.construct_from_result(result, "Publisher", "publishbook");
                self.workflow.add(task);
            elif result.data['editstatus'] == "Revise":
                task = Task.construct_from_result(result, "Editor", "editmanuscript");
                self.workflow.add(task);
            else:
                pass
            self.workflow.update_status(result, Status.COMPLETE);
            
#    def Set_price_complete(self, results):
#                for result in results:
#                    task = Task.construct_from_result(result, "Publisher", "publishbook");
#                    self.workflow.add(task);
#                    self.workflow.update_status(result, Status.COMPLETE);   
                    
    def publishing_complete(self, results):
        for result in results:
            self.workflow.update_status(result, Status.COMPLETE);                
            

if __name__ == '__main__':
    backend = PublishingBackend();